<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-08 15:20:41
 * @Last Modified time: 2023-02-21 17:36:04
 */
namespace app\common\model\store;

use frank\base\BaseModel;
use frank\traits\ModelTrait;
use app\common\enum\EnumFactory;
use app\common\services\RegionService;

/**
 * 门店
 * Class ShopModel
 * @package app\common\model\store
 */
class ShopModel extends BaseModel
{
    use ModelTrait;

    /**
     * 数据表主键
     */
    protected $pk = 'id';

    /**
     * 模型名称
     */
    protected $name = 'store_shop';


    /**
     * 追加字段
     * @var array
     */
    protected $append = ['region','full_address','coordinate'];

    /**
     * 地区名称
     */
    public function getRegionAttr($value, $data)
    {
        $regionService = RegionService::instance();
        return [
            'province' => $regionService->getNameById($data['province_id']),
            'city' => $regionService->getNameById($data['city_id']),
            'region' => $data['region_id'] == 0 ? '' : $regionService->getNameById($data['region_id']),
        ];
    }

    public function getFullAddressAttr($value, $data)
    {
         return $this['region']['province'] . $this['region']['city'] . $this['region']['region'] . $this['detail'];
    }
    
    /**
     * 状态修改器
     */
    public function getStatusAttr($value)
    {
        return EnumFactory::instance('status')->getItem($value);
    }

    public function getCoordinateAttr($value,$data)
    {
        $latitude = '';
        $longitude = '';
        $arr = explode(',', $data['location']);
        !empty($arr[0]) && $longitude = $arr[0];
        !empty($arr[1]) && $latitude = $arr[1];
        return ['latitude' => $latitude, 'longitude' => $longitude];
    }
}

